package net.microfigure.controller;

import javax.validation.Valid;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.microfigure.model.ExpInst;
import net.microfigure.model.IllCard;
import net.microfigure.service.IExpInstService;
import net.microfigure.service.IIllCardService;
import net.microfigure.commons.base.BaseController;
import net.microfigure.commons.result.PageInfo;
import net.microfigure.commons.utils.IDUtils;

/**
 * <p>
 * 报销实例表 前端控制器
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-25
 */
@Controller
@RequestMapping("/expInst")
public class ExpInstController extends BaseController {

	@Autowired 
	private IExpInstService expInstService;
	@Autowired
	private IIllCardService illCardService;
	@GetMapping("/manager")
	public String manager() {
		return "admin/expInst/expInstList";
	}



	/**
	 * 分页
	 * @return
	 */
	@PostMapping("/dataGrid")
	@ResponseBody
	public Object dataGrid(Integer page,Integer rows,String sort, String order) {
		PageInfo pageInfo = new PageInfo(page, rows, sort, order);
		expInstService.selectDataGrid(pageInfo);
		return pageInfo;
	}

	/**
	 * 添加报销实例表信息跳转
	 * 
	 * @param model
	 */
	@GetMapping("/addPage")
	public String addPage(Model model, String cardNo) {

		List<IllCard> illCards = illCardService.selectListByCardNo(cardNo);
		ExpInst expInst = new ExpInst();
		if (!CollectionUtils.isEmpty(illCards)) {
			IllCard illCard = illCards.get(0);
			expInst.setCardNo(illCard.getCardNo());
			expInst.setIllNo(illCard.getIllCardNo());
			expInst.setTreateinNo(IDUtils.genItemId());
		}
		model.addAttribute("expInst", expInst);


		return "admin/expInst/expInstAdd";
	}

	/**
	 * 添加
	 * @param 
	 * @return
	 */
	@PostMapping("/add")
	@ResponseBody
	public Object add(@Valid ExpInst expInst) {
		System.out.println("判断时间"+expInst.getRecodTime());



		List<IllCard> list=illCardService.equalsByRecodTime(expInst.getRecodTime());
		if(list!=null) {
			System.out.println("符合就诊");
			
			expInstService.insert(expInst);
			return renderSuccess("添加成功！");
		}else {
			return renderError("添加失败！原因时间不符合报销");
		}


	}

	/**
	 * 计算校验报销实例
	 * 
	 * @param ExpInst
	 * @param model
	 */
	@RequestMapping(value = "precreate", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ExpInst precreate(@Valid ExpInst expInst) {
		System.out.println("计算校验报销实例"+expInst.getAllCost());
		ExpInst preExpInst = expInstService.preSubmitExpense(expInst);
		return preExpInst;
	}



	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(Long expInstNo) {

		boolean b = expInstService.deleteById(expInstNo);
		if (b) {
			return renderSuccess("删除成功！");
		} else {
			return renderError("删除失败！");
		}
	}

	/**
	 * 编辑
	 * @param model
	 * @param id
	 * @return
	 */
	@GetMapping("/editPage")
	public String editPage(Model model, Long id) {
		ExpInst expInst = expInstService.selectById(id);
		model.addAttribute("expInst", expInst);
		return "admin/expInst/expInstEdit";
	}

	/**
	 * 编辑
	 * @param 
	 * @return
	 */
	@PostMapping("/edit")
	@ResponseBody
	public Object edit(@Valid ExpInst expInst) {

		boolean b = expInstService.updateById(expInst);
		if (b) {
			return renderSuccess("编辑成功！");
		} else {
			return renderError("编辑失败！");
		}
	}
}
